package com.hh.planCategory.service.impl;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/25
 * @VERSON:1.8
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hh.commen.PageVO;
import com.hh.planCategory.vo.AddPlanCategoryVo;
import com.hh.planCategory.vo.PlanCategoryVo;
import com.hh.planCategory.vo.StatusChangeVo;
import com.hh.planCategory.vo.UpdatePlanCategoryVo;
import com.hh.planCategory.bean.PlanCategory;
import com.hh.planCategory.mapper.PlanCategoryMapper;
import com.hh.planCategory.service.PlanCategoryService;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PlanCategoryServiceImpl implements PlanCategoryService {
    @Autowired
    private PlanCategoryMapper planCategoryMapper;
    @Override
    public PageVO findAllByPage(PlanCategoryVo planCategoryVo) {
        PageHelper.startPage((planCategoryVo.getPageIndex()), planCategoryVo.getPageSize());
        List<PlanCategory> planCategoryList = planCategoryMapper.selectAllPlanCategory(planCategoryVo);
        PageInfo<PlanCategory> pageInfo = new PageInfo<>(planCategoryList);
        PageVO pageVO = new PageVO();
        pageVO.setData(planCategoryList);
        pageVO.setTotalNumber(pageInfo.getTotal());
        pageVO.setTotalPage(pageInfo.getPageNum());
        return pageVO;
    }

    @Override
    public Boolean updateStatus(StatusChangeVo statusChangeVo) {
        int row = planCategoryMapper.updateStatusById(statusChangeVo);
        if (row == 1) {
            return true;
        }
        return false;
    }

    @Override
    public ResultVo addPlanCategory(AddPlanCategoryVo addPlanCategoryVo) {
        if (planCategoryMapper.judgeCodeExist(addPlanCategoryVo.getCode()) != null) {
            return ResultVo.error("备件大类编码重复");
        }
        if (planCategoryMapper.judgeNameExist(addPlanCategoryVo.getName()) != null) {
            return ResultVo.error("备件大类名称重复");
        }
        PlanCategory planCategory = new PlanCategory();
        planCategory.setPlanCategoryCode(addPlanCategoryVo.getCode());
        planCategory.setPlanCategoryName(addPlanCategoryVo.getName());

        if (addPlanCategoryVo.getStatus()) {
            planCategory.setStatus(1);
        }else {
            planCategory.setStatus(0);
        }
        planCategory.setRemark(addPlanCategoryVo.getRemark());
        Date date = new Date();
        planCategory.setCreateTime(date);
        planCategory.setUpdateTime(date);

        int row = planCategoryMapper.insert(planCategory);
        if (row == 1) {
            return ResultVo.success();
        }
        return ResultVo.error("添加失败");
    }

    @Override
    public Boolean updatePlanCategory( UpdatePlanCategoryVo updatePlanCategoryVo) {
        if (planCategoryMapper.judgeUpdateName(updatePlanCategoryVo.getId(), updatePlanCategoryVo.getName()) != null) {
            return false;
        }
        PlanCategory planCategory = new PlanCategory();
        planCategory.setId(updatePlanCategoryVo.getId());
        planCategory.setPlanCategoryCode(updatePlanCategoryVo.getCode());
        planCategory.setPlanCategoryName(updatePlanCategoryVo.getName());
        planCategory.setStatus(updatePlanCategoryVo.getStatus());
        planCategory.setRemark(updatePlanCategoryVo.getRemark());
        Date date = new Date();
        planCategory.setUpdateTime(date);
        planCategory.setVersion(updatePlanCategoryVo.getVersion());
        int row = planCategoryMapper.updateByPrimaryKey(planCategory);
        if (row == 1) {
            return true;
        }
        return false;
    }

    @Override
    public ResultVo getAllPlanCategoryList() {
        return ResultVo.success(planCategoryMapper.selectAllAsList());
    }

}
